home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 November / PCWorld_2007-11_cd.bin / v cisle / dap / dap85.exe / %TEMPCABFULLDIR% / DapLang.cab / dapservice.js < prev    next >
Encoding:
Text File  |  2006-10-25  |  13.9 KB  |  558 lines

  1. const gDap_ConsoleService =
  2.       Components.
  3.           classes['@mozilla.org/consoleservice;1'].
  4.               getService(Components.interfaces.nsIConsoleService);
  5. var bLog  = false;
  6.  
  7.  
  8. function IsLog()
  9. {
  10.   try
  11.     {
  12.      var obj = Components.classes["@speedbit.com/dapfirefox/dapcomponent;1"].createInstance();
  13.      obj = obj.QueryInterface(Components.interfaces.IDAPComponent);
  14.      return obj.IsWriteLog();
  15.     
  16.     }
  17.     catch(ex)
  18.    {
  19.  
  20.    }
  21.   return false;
  22. }
  23.  
  24. function LOG(aMessage)
  25. {
  26.    if( bLog == false  )
  27.     bLog = IsLog();
  28.  
  29.   if( bLog == false )
  30.     return;
  31.  
  32.   gDap_ConsoleService.logStringMessage('DAP Service: ' + aMessage);
  33. }
  34.  
  35. const URI_DATA_LOCALE = "chrome://dapff/locale/dapff.properties"; 
  36. const EXTENSION_ID = "{F17C1572-C9EC-4e5c-A542-D05CBB5C5A08}";
  37. ///////////////////////////////////////////////////////////////////////////////////////////////////
  38.  
  39. function DAPService()
  40. {
  41.     this._init();
  42. }
  43.  
  44.  
  45. DAPService.prototype =
  46. {
  47.  
  48. bRegular:0,
  49. bMinIntegration:0,
  50.     // nsISupports ============================================================
  51.  
  52.     //-------------------------------------------------------------------------
  53.     QueryInterface: function(iid)
  54.     {
  55.         if (iid.equals(Components.interfaces.nsIURIContentListener) ||
  56.             iid.equals(Components.interfaces.nsISupportsWeakReference) ||
  57.             iid.equals(Components.interfaces.nsIFactory) ||
  58.             iid.equals(Components.interfaces.nsISupports) ||
  59.             iid.equals(Components.interfaces.nsIObserver ) )
  60.         {
  61.             return this;
  62.         }
  63.         throw Components.results.NS_ERROR_NO_INTERFACE;
  64.     },
  65.  
  66.  
  67.     // nsIFactory =============================================================
  68.     createInstance: function(outer, iid)
  69.     {
  70.         LOG("Create instans");
  71.         if (null != outer)
  72.         {
  73.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  74.         }
  75.         return this.QueryInterface(iid);
  76.     },
  77.     shoudtrig : function ()
  78.     {
  79.             // user choose in dialog regular speed 
  80.             if( this.isRegular() )
  81.                 return false;
  82.             // user unintegrate with browser , only context menu 
  83.             if( this.isMinIntegration() )
  84.                 return false;
  85.  
  86.             return false;
  87.  
  88.             /*try
  89.                {
  90.               var obj = this.getDAPComponent();
  91.               if( !obj )
  92.               {
  93.                 LOG("DAP not exist");
  94.                 return false;
  95.               }
  96.               // problem run dap
  97.               if( !this._runDAP() )
  98.                 return false;
  99.  
  100.               if( obj.IsAccelerate() )
  101.                 return true;
  102.               
  103.               this.setRegularDownload(true);
  104.               return false;
  105.             }
  106.             catch(ex)
  107.             {
  108.                 LOG(ex);
  109.             }*/
  110.     
  111.     },
  112.     setRegularDownload : function (value)
  113.     {
  114.         LOG("setRegularDownload");
  115.         this.bRegular = value;
  116.     },
  117.     isRegular : function ()
  118.     {
  119.         LOG("isRegular");
  120.         return this.bRegular;
  121.     },
  122.     isMinIntegration : function()
  123.     {
  124.         LOG("min Integration");
  125.         return this.bMinIntegration ;
  126.     },
  127.     unregister :function ()
  128.     {
  129.       const osvr=Components.classes['@mozilla.org/observer-service;1'].getService(
  130.                                   Components.interfaces.nsIObserverService);
  131.        osvr.removeObserver(this,"xpcom-shutdown");
  132.        osvr.removeObserver(this,"em-action-requested",false);
  133.     },
  134.     getDAPComponent : function()
  135.     {
  136.             return  Components.classes["@speedbit.com/dapfirefox/dapcomponent;1"].createInstance().
  137.                             QueryInterface(Components.interfaces.IDAPComponent);
  138.     },
  139.     checkDAPIntegr: function()
  140.     {
  141.         const nsIWindowsRegKey = Components.interfaces.nsIWindowsRegKey;
  142.         var key = Components.classes["@mozilla.org/windows-registry-key;1"]
  143.                 .createInstance(nsIWindowsRegKey);
  144.  
  145.         var path = "Software\\SpeedBit\\Download Accelerator";
  146.         var root = nsIWindowsRegKey.ROOT_KEY_CURRENT_USER;
  147.         try
  148.          {
  149.             key.open(root, path, nsIWindowsRegKey.ACCESS_READ);
  150.             this.bMinIntegration = key.readIntValue("FFMinIntegration");
  151.             LOG(this.bMinIntegration);
  152.         } catch(e)
  153.          {
  154.             LOG(e);
  155.         }
  156.             key.close();
  157.  
  158.     },
  159.     _setToDap : function( value )
  160.     {
  161.        LOG("Send to DAP");
  162.        LOG(value);
  163.        try
  164.           {
  165.           var obj = this.getDAPComponent();
  166.           if( obj )
  167.             obj.SetExtentionState(value);
  168.         }
  169.        catch(ex)
  170.        {
  171.         LOG(ex);
  172.          }
  173.  
  174.     },
  175.     observe: function(subject, topic, data) 
  176.     {
  177.         LOG("observe");
  178.         switch(topic) 
  179.         {
  180.             case "xpcom-shutdown":
  181.                   this.unregister();
  182.                   break;
  183.         case  "em-action-requested" :
  184.                if( (subject instanceof Components.interfaces.nsIUpdateItem)
  185.                 && subject.id==EXTENSION_ID )
  186.            {
  187.               if( data == "item-disabled" )
  188.                  this._setToDap(true);
  189.                    if( data == "item-cancel-action" )
  190.                this._setToDap(false);
  191.             if( data == "item-enabled" )
  192.             {
  193.                this._setToDap(false);
  194.                  LOG("enable");
  195.               }
  196.  
  197.              break;
  198.            }
  199.         }
  200.     },
  201.  
  202.  
  203.  
  204.     // nsIURIContentListener ==================================================
  205.  
  206.     //-------------------------------------------------------------------------
  207.     canHandleContent: function(contentType, isContentPreferred, desiredContentType)
  208.     {
  209.         
  210.         return this.shoudtrig();
  211.     },
  212.     
  213.     
  214.     //-------------------------------------------------------------------------
  215.     doContent: function(contentType, isContentPreferred, request, contentHandler)
  216.     {
  217.         LOG(request.name);
  218.         try
  219.            {
  220.             request.QueryInterface(Components.interfaces.nsIChannel);
  221.             const uri = request.URI;
  222.  
  223.                 var obj = this.getDAPComponent();
  224.  
  225.                 if( obj && obj.IsShoudTrigger(request.name) )
  226.                     obj.SendToDAP(request.name,uri.host,"","",window.navigator.userAgent);
  227.  
  228.                 // cancel request                 
  229.                 contentHandler.value=null;
  230.                 request.cancel(Components.results.NS_BINDING_ABORTED);
  231.  
  232.            }
  233.            catch(ex)
  234.            {
  235.                     LOG(ex);
  236.            }
  237.  
  238.         return true;
  239.     },
  240.     
  241.     
  242.     //-------------------------------------------------------------------------
  243.     isPreferred: function(contentType, desiredContentType)
  244.     {
  245.         return this.shoudtrig();
  246.     },
  247.     
  248.     
  249.     //-------------------------------------------------------------------------
  250.     onStartURIOpen: function(uri)
  251.     {
  252.             // false == abort load
  253.         return false;
  254.     },
  255.  
  256.  
  257.     // Our Stuff ==============================================================
  258.  
  259.     //-------------------------------------------------------------------------
  260.     get wrappedJSObject()    { LOG("wrappedJSObject"); return this; },
  261.     _checkDAP : function()
  262.     {
  263.         
  264.         var window  = Components.classes['@mozilla.org/appshell/window-mediator;1']
  265.                          .getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow (null);
  266.         if( !window )
  267.         {
  268.           LOG("Window is null");
  269.          return ;
  270.         }
  271.  
  272.         const nsIWindowsRegKey = Components.interfaces.nsIWindowsRegKey;
  273.         var key = Components.classes["@mozilla.org/windows-registry-key;1"]
  274.                 .createInstance(nsIWindowsRegKey);
  275.  
  276.         var path = "Software\\SpeedBit\\Download Accelerator";
  277.         var root = nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE;
  278.         var appPath;
  279.  
  280.         try
  281.          {
  282.             key.open(root,path,nsIWindowsRegKey.ACCESS_READ);
  283.             appPath = key.readStringValue("EXELOCATION");
  284.             key.close();
  285.  
  286.  
  287.             var exeFile = Components.classes["@mozilla.org/file/local;1"]    .createInstance(Components.interfaces.nsILocalFile);
  288.                     
  289.             exeFile.initWithPath(appPath);
  290.             if ( !exeFile.exists() )
  291.             {    
  292.                 LOG("File not exists");
  293.                 window.openDialog("chrome://dapff/content/confirm.xul","_blank","chrome,centerscreen,resizable=no");
  294.                 return false;
  295.             }
  296.  
  297.             
  298.             
  299.          }
  300.          catch(ex)
  301.         {
  302.             LOG(ex);
  303.             if( !appPath )
  304.             {
  305.               window.openDialog("chrome://dapff/content/confirm.xul","_blank","chrome,centerscreen,resizable=no");
  306.               return false;
  307.             }
  308.  
  309.         }
  310.         return true;
  311.     },
  312.     getDAPExeLocation : function()
  313.     {
  314.         
  315.         const nsIWindowsRegKey = Components.interfaces.nsIWindowsRegKey;
  316.         var key = Components.classes["@mozilla.org/windows-registry-key;1"]
  317.                 .createInstance(nsIWindowsRegKey);
  318.  
  319.         var path = "Software\\SpeedBit\\Download Accelerator";
  320.         var root = nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE;
  321.         var appPath;
  322.         try
  323.         {
  324.              key.open(root,path,nsIWindowsRegKey.ACCESS_READ);
  325.             appPath = key.readStringValue("EXELOCATION");
  326.             key.close();
  327.  
  328.         }
  329.         catch(ex)
  330.         {
  331.           LOG("get Dap exelocation");
  332.           LOG(ex);
  333.           return null;
  334.         }
  335.         return appPath;
  336.  
  337.     },
  338.     showalert : function()
  339.     {
  340.         if( this.isRegular() )
  341.          return;
  342.  
  343.         var prompt = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  344.             .getService(Components.interfaces.nsIPromptService);
  345.           LOG("Prompt");
  346.              var localeService = Components.classes["@mozilla.org/intl/nslocaleservice;1"].
  347.                         getService(Components.interfaces.nsILocaleService); 
  348.         var appLocale = localeService.getApplicationLocale( ); 
  349.  
  350.         var sbs =  Components.classes["@mozilla.org/intl/stringbundle;1"].
  351.                         getService(Components.interfaces.nsIStringBundleService);
  352.  
  353.         var Bundle = sbs.createBundle(URI_DATA_LOCALE);
  354.         if( !Bundle )
  355.         {
  356.           LOG("Bundle is null");
  357.           return;                                        
  358.         }
  359.         var Message =  Bundle.GetStringFromName("dapff.text-message");
  360.  
  361.          prompt.alert(null,"DAP",Message);
  362.          this.setRegularDownload(true);
  363.  
  364.     },
  365.     _runDAP : function()
  366.     {
  367.         LOG("RUN DAP");
  368.  
  369.  
  370.         var appPath = this.getDAPExeLocation();
  371.         
  372.         if( !appPath )
  373.         {
  374.             this.showalert();
  375.             return false;
  376.         }
  377.  
  378.  
  379.         try
  380.          {
  381.  
  382.             var exeFile = Components.classes["@mozilla.org/file/local;1"]    .createInstance(Components.interfaces.nsILocalFile);
  383.                     
  384.             exeFile.initWithPath(appPath);
  385.             if ( !exeFile.exists() )
  386.             {
  387.               this.showalert();
  388.               return false;        
  389.             }
  390.             //var app =     Components.classes["@mozilla.org/process/util;1"]
  391.             //            .createInstance(Components.interfaces.nsIProcess);
  392.                         
  393.             //app.init(exeFile);
  394.             //app.run(false," /HIDE /ID=DAPFF",0);
  395.                 
  396.         }
  397.         catch(ex)
  398.         {
  399.           LOG("Catch runDAP");
  400.           LOG(ex);
  401.  
  402.           this.showalert();    
  403.                return false;
  404.           
  405.         }
  406.         return true;
  407.     },
  408.     //-------------------------------------------------------------------------
  409.     _init: function()
  410.     {
  411.         
  412.             // Register this as content listener
  413.         Components.classes["@mozilla.org/uriloader;1"]
  414.             .getService(Components.interfaces.nsIURILoader)
  415.             .registerContentListener(this);
  416.            const osvr=Components.classes['@mozilla.org/observer-service;1'].getService(
  417.                 Components.interfaces.nsIObserverService);
  418.               osvr.addObserver(this,"xpcom-shutdown",false);
  419.  
  420.          osvr.addObserver(this,"em-action-requested",false);
  421.  
  422.         this.setlocalstringToComponent();
  423.         this.checkDAPIntegr();
  424.           this._setToDap(false);
  425.     },
  426.       setlocalstringToComponent : function()
  427.     {
  428.             LOG("setlocalstringToComponent ");
  429.             try
  430.             {
  431.  
  432.                             var localeService = Components.classes["@mozilla.org/intl/nslocaleservice;1"].
  433.                                                                     getService(Components.interfaces.nsILocaleService); 
  434.                             var appLocale = localeService.getApplicationLocale( ); 
  435.  
  436.                             var sbs =  Components.classes["@mozilla.org/intl/stringbundle;1"].
  437.                                                     getService(Components.interfaces.nsIStringBundleService);
  438.  
  439.                             var Bundle = sbs.createBundle(URI_DATA_LOCALE);
  440.                             if( !Bundle )
  441.                             {
  442.                                 LOG("Bundle is null");
  443.                                 return;                                        
  444.                             }
  445.                             var TotalString ;
  446.  
  447.                              var title = Bundle.GetStringFromName("dapff.title-choose-dlg");
  448.                             TotalString = title + '╢';
  449.                             title = Bundle.GetStringFromName("dapff.text-choose-dlg");
  450.                             TotalString = TotalString + title + '╢';
  451.                             title = Bundle.GetStringFromName("dapff.button-regular");
  452.                             TotalString = TotalString + title + '╢';
  453.                             title = Bundle.GetStringFromName("dapff.button-acceler");            
  454.                             TotalString = TotalString + title + '╢';
  455.             
  456.                             title = Bundle.GetStringFromName("dapff.title-add-black-list");
  457.                             TotalString = TotalString + title + '╢';
  458.                             title = Bundle.GetStringFromName("dapff.text-add-black-list");
  459.                             TotalString = TotalString + title + '╢';
  460.                             title = Bundle.GetStringFromName("dapff.text-check-add-black-list");
  461.                             TotalString = TotalString + title + '╢';
  462.                             title = Bundle.GetStringFromName("dapff.button-yes");
  463.                             TotalString = TotalString + title +'╢';
  464.                             title = Bundle.GetStringFromName("dapff.button-no");
  465.                             TotalString = TotalString + title ;
  466.  
  467.                             var obj = this.getDAPComponent();
  468.                             if( obj )
  469.                                  obj.SetLocalizationString(TotalString);
  470.  
  471.         }
  472.         catch( ex )
  473.         {
  474.             LOG(ex);
  475.         }
  476.     },
  477.   
  478. };
  479.  
  480.  
  481. //=============================================================================
  482.  
  483. var module =
  484. {
  485.     _cid:            Components.ID("{13AC88E5-B176-491a-8D8F-73473A0A57EF}"),
  486.     _contractId:    "@speedbit/dapcomponent-service;1",
  487.     _name:            "DAP Integration",
  488.  
  489.     
  490.     _isRegistered:    false,
  491.     
  492.     _factory:        {
  493.                         _instance: null,
  494.                         
  495.                         createInstance: function(outer, iid)
  496.                         {
  497.                             LOG("createInstance");
  498.                             if (null != outer)
  499.                             {
  500.                                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  501.                             }
  502.                             if (null == this._instance)
  503.                             {
  504.                                 this._instance = (new DAPService()).QueryInterface(iid);
  505.                             }
  506.                             return this._instance;
  507.                         }
  508.                     },
  509.                     
  510.                     
  511.     //-------------------------------------------------------------------------
  512.     registerSelf: function(compMgr, fileSpec, location, type)
  513.     {
  514.         if (this._isRegistered)
  515.         {
  516.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  517.         }
  518.     
  519.         this._isRegistered = true;
  520.     
  521.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  522.         compMgr.registerFactoryLocation(this._cid, this._name, this._contractId,
  523.                                         fileSpec, location, type);
  524.     },
  525.     
  526.     
  527.     //-------------------------------------------------------------------------
  528.     getClassObject: function(compMgr, cid, iid)
  529.     {
  530.         if (!cid.equals(this._cid))
  531.         {
  532.             throw Components.results.NS_ERROR_NO_INTERFACE;
  533.         }
  534.         
  535.         if (!iid.equals(Components.interfaces.nsIFactory))
  536.         {
  537.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  538.         }
  539.         return this._factory;
  540.     },
  541.     
  542.     
  543.     //-------------------------------------------------------------------------
  544.     canUnload: function(compMgr)
  545.     {
  546.         return true;
  547.     },
  548. };
  549.  
  550.  
  551. function NSGetModule(compMgr, fileSpec)
  552. {
  553.     LOG("NSGetModule");
  554.     return module;
  555. }
  556.  
  557. //=============================================================================
  558.